#include "stdafx.h"
#include "ClickDetector.h"

int ClickDetector::GetClicks()
{
	UpdateClicks();
	int clicks = 1;

	list<DWORD>::reverse_iterator ti = timeList.rbegin();
	list<short>::reverse_iterator xi = xPosList.rbegin();
	list<short>::reverse_iterator yi = yPosList.rbegin();
	for(int i = 0; i < maxClicks - 1; i++)
	{
		DWORD nextTime = *ti++;
		short nextX = *xi++;
		short nextY = *yi++;

		if((nextTime - *ti) < dblClickTime &&
			(nextX - *xi) < dblClickX &&
			(nextY - *yi) < dblClickY)
		{
			clicks++;
			continue;
		}
		break;
	}
	return clicks;
}

ClickDetector::ClickDetector(int supportingClicks)
{
	maxClicks = supportingClicks;
	dblClickTime = GetDoubleClickTime();
	dblClickX = GetSystemMetrics(SM_CXDOUBLECLK);
	dblClickY = GetSystemMetrics(SM_CYDOUBLECLK);

	for(int i = 0; i < maxClicks; i++)
	{
		timeList.push_back(i + dblClickTime * 2 - INT_MIN);
		xPosList.push_back(i + dblClickX * 2 - INT_MIN);
		yPosList.push_back(i + dblClickY * 2 - INT_MIN);
	}
}

void ClickDetector::UpdateClicks()
{
	timeList.push_back(GetMessageTime());
	xPosList.push_back(LOWORD(GetMessagePos()));
	yPosList.push_back(HIWORD(GetMessagePos()));
	timeList.pop_front();
	xPosList.pop_front();
	yPosList.pop_front();
}